package com.smart.domain;

import com.smart.dao.DBoperator;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.LinkedList;
import javafx.print.Collation;

/*学生的增删改查*/
public class StudentUserDaoimpl implements StudentUserDao {

    @Override
    public StudentUser select(String id) {

        String SQL="select * from STUDENT where id = ?";
        DBoperator stmt = null;
        StudentUser user = new StudentUser();

        try{
            stmt = new DBoperator();
            Connection coon = stmt.getConnection();
            PreparedStatement statement1 = coon.prepareStatement(SQL);
            statement1.setString(1,id);
            ResultSet resultSet = statement1.executeQuery();
            while(resultSet.next()){
                user.setName(resultSet.getString("name"));
                user.setId(resultSet.getString("id"));
                user.setSclass(resultSet.getString("sclass"));
                user.setGrade(resultSet.getString("grade"));
                user.setPassword(resultSet.getString("password"));
                user.setSex(resultSet.getString("sex"));
            }
            resultSet.close();
            statement1.close();
            coon.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }
    @Override
    public List<StudentUser> selectclass(String sclass) {
        List<StudentUser> list=new ArrayList<StudentUser>();
        String SQL="select * from stu where sclass=?";
        DBoperator stmt = null;
        stmt = new DBoperator();
        Connection coon = stmt.getConnection();
        try {
            PreparedStatement statement1 = coon.prepareStatement(SQL);
            statement1.setString(1,sclass);
            ResultSet resultSet = statement1.executeQuery();
            while(resultSet.next()){
                StudentUser user = new StudentUser();//每一次都要重新new一个
                user.setName(resultSet.getString("name"));
                user.setId(resultSet.getString("id"));
                user.setSex(resultSet.getString("sex"));
                user.setGrade(resultSet.getString("grade"));
                user.setSclass(resultSet.getString("sclass"));
                user.setPassword(resultSet.getString("password"));
                list.add(user);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        //Collections.sort(list);

        return list;
    }
}
